package com.app.service.impl;

import com.app.domain.bo.NoticeBo;
import com.app.domain.page.Page;
import com.app.domain.po.NoticePo;
import com.app.domain.vo.NoticeVo;
import com.app.framework.utils.CoBeanUtils;
import com.app.mapper.NoticeMapper;
import com.app.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * 公告方法实现
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticePo> implements NoticeService {

    /**
     * 公告列表
     *
     * @param bo 入参
     */
    @Override
    @Cacheable(cacheNames = "notice", key = "#bo.getPageNum")
    public Page<NoticeVo> lists(NoticeBo bo) {
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        List<NoticeVo> noticeVos = baseMapper.lists();
        return Page.restPage(noticeVos);
    }

    /**
     * 添加公告
     *
     * @param bo 入参
     */
    @Override
    @CacheEvict(cacheNames = "notice", allEntries = true)
    public void saveOne(NoticeBo bo) {
        NoticePo noticePo = CoBeanUtils.copy(bo, NoticePo.class);
        noticePo.setCreateTime(new Date());
        baseMapper.insert(noticePo);
    }

    /**
     * 修改公告
     *
     * @param bo 入参
     */
    @Override
    @CacheEvict(cacheNames = "notice", allEntries = true)
    public void updateOne(NoticeBo bo) {
        NoticePo noticePo = baseMapper.selectById(bo.getId());
        Assert.notNull(noticePo, "该公告不存在");
        CoBeanUtils.copyProperties(bo, noticePo);
        noticePo.setUpdateTime(new Date());
        baseMapper.updateById(noticePo);
    }

    /**
     * 删除公告
     *
     * @param noticeId 公告ID
     */
    @Override
    @CacheEvict(cacheNames = "notice", allEntries = true)
    public void deleteOne(Long noticeId) {
        NoticePo noticePo = baseMapper.selectById(noticeId);
        Assert.notNull(noticePo, "该公告不存在");
        baseMapper.deleteById(noticeId);
    }

}